package com.haohan.jingyuan.user.controller;

import cn.hutool.json.JSONUtil;
import com.haohan.jingyuan.gateway.aspect.NeedLogin;
import com.haohan.jingyuan.gateway.dto.GwRequest;
import com.haohan.jingyuan.gateway.dto.GwResponse;
import com.haohan.jingyuan.user.dto.req.CreateUserRequest;
import com.haohan.jingyuan.user.dto.req.DeleteUserRequest;
import com.haohan.jingyuan.user.dto.req.FreezeUserRequest;
import com.haohan.jingyuan.user.dto.req.LoginRequest;
import com.haohan.jingyuan.user.dto.req.LogoutRequest;
import com.haohan.jingyuan.user.dto.req.QueryUserListRequest;
import com.haohan.jingyuan.user.dto.req.ResetPasswordRequest;
import com.haohan.jingyuan.user.dto.req.UpdatePasswordRequest;
import com.haohan.jingyuan.user.dto.req.UpdateUserRequest;
import com.haohan.jingyuan.user.dto.resp.LoginResp;
import com.haohan.jingyuan.user.dto.resp.UserInfoResp;
import com.haohan.jingyuan.user.dto.resp.UserListResp;
import com.haohan.jingyuan.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 用户控制器
 *
 * @ClassName UserController
 * @Description 用户相关接口控制器
 * @Author hexijun
 * @Time 2025/01/18 22:20
 * @Version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class UserController {

    /**
     * 用户服务
     */
    private final UserService userService;

    /**
     * 创建用户
     *
     * @param request 创建用户请求
     * @return 创建结果
     */
    @NeedLogin
    @PostMapping("/admin/user/create")
    public GwResponse<UserInfoResp> createUser(@Valid @RequestBody GwRequest<CreateUserRequest> request) {
        log.info("UserController.createUser BEGIN ==> {}", JSONUtil.toJsonStr(request));

        UserInfoResp userInfo = userService.createUser(request.getRequestData());
        GwResponse<UserInfoResp> response = GwResponse.success("创建用户成功", userInfo);

        log.info("UserController.createUser END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 重置用户密码
     *
     * @param request 重置密码请求
     * @return 重置结果
     */
    @NeedLogin
    @PostMapping("/admin/user/resetPassword")
    public GwResponse<UserInfoResp> resetPassword(@Valid @RequestBody GwRequest<ResetPasswordRequest> request) {
        log.info("UserController.resetPassword BEGIN ==> {}", JSONUtil.toJsonStr(request));

        UserInfoResp userInfo = userService.resetPassword(request.getRequestData());
        GwResponse<UserInfoResp> response = GwResponse.success("重置密码成功", userInfo);

        log.info("UserController.resetPassword END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 冻结用户
     *
     * @param request 冻结用户请求
     * @return 冻结结果
     */
    @NeedLogin
    @PostMapping("/admin/user/freeze")
    public GwResponse<UserInfoResp> freezeUser(@Valid @RequestBody GwRequest<FreezeUserRequest> request) {
        log.info("UserController.freezeUser BEGIN ==> {}", JSONUtil.toJsonStr(request));

        UserInfoResp userInfo = userService.freezeUser(request.getRequestData());
        GwResponse<UserInfoResp> response = GwResponse.success("冻结用户成功", userInfo);

        log.info("UserController.freezeUser END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 查询用户列表
     *
     * @param request 查询用户列表请求
     * @return 用户列表
     */
    @NeedLogin
    @PostMapping("/admin/user/list")
    public GwResponse<UserListResp> queryUserList(@Valid @RequestBody GwRequest<QueryUserListRequest> request) {
        log.info("UserController.queryUserList BEGIN ==> {}", JSONUtil.toJsonStr(request));

        UserListResp userList = userService.queryUserList(request.getRequestData());
        GwResponse<UserListResp> response = GwResponse.success("查询用户列表成功", userList);

        log.info("UserController.queryUserList END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 用户登录
     *
     * @param request 登录请求（用户名与密码）
     * @return 登录成功返回token，失败返回错误信息
     */
    @PostMapping("/user/login")
    public GwResponse<LoginResp> userLogin(@Valid @RequestBody GwRequest<LoginRequest> request) {
        log.info("UserController.userLogin BEGIN ==> {}", JSONUtil.toJsonStr(request));

        LoginResp loginResp = userService.login(request.getRequestData());
        GwResponse<LoginResp> response = null;
        if (loginResp == null) {
            response = GwResponse.error("用户名或密码错误");
        } else {
            response = GwResponse.success("登录成功", loginResp);
        }

        log.info("UserController.userLogin END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 用户登出
     *
     * @param request 登出请求（包含token）
     * @return 登出成功或失败信息
     */
    @NeedLogin
    @PostMapping("/user/logout")
    public GwResponse<String> userLogout(@Valid @RequestBody GwRequest<LogoutRequest> request) {
        log.info("UserController.userLogout BEGIN ==> {}", JSONUtil.toJsonStr(request));

        boolean success = userService.logout();
        GwResponse<String> response = null;
        if (success) {
            response = GwResponse.success("登出成功", null);
        } else {
            response = GwResponse.error("登出失败");
        }

        log.info("UserController.userLogout END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 修改用户密码
     *
     * @param request     修改密码请求（包含用户ID和新密码）
     * @return 修改成功或失败信息
     */
    @NeedLogin
    @PostMapping("/user/updatePwd")
    public GwResponse<String> updatePassword(@Valid @RequestBody GwRequest<UpdatePasswordRequest> request) {
        log.info("UserController.updatePassword BEGIN ==> {}", JSONUtil.toJsonStr(request));

        boolean success = userService.updatePassword(request.getRequestData());
        GwResponse<String> response = null;
        if (success) {
            response = GwResponse.success("修改密码成功", null);
        } else {
            response = GwResponse.error("修改密码失败");
        }

        log.info("UserController.updatePassword END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 编辑用户信息
     *
     * @param request 编辑用户请求
     * @return 用户信息
     */
    @NeedLogin
    @PostMapping("/admin/user/update")
    public GwResponse<UserInfoResp> updateUser(@Valid @RequestBody GwRequest<UpdateUserRequest> request) {
        log.info("UserController.updateUser BEGIN ==> {}", JSONUtil.toJsonStr(request));

        UserInfoResp userInfo = userService.updateUser(request.getRequestData());
        GwResponse<UserInfoResp> response = GwResponse.success("编辑用户成功", userInfo);

        log.info("UserController.updateUser END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 删除用户信息
     *
     * @param request 删除用户请求
     * @return 删除结果
     */
    @NeedLogin
    @PostMapping("/admin/user/delete")
    public GwResponse<String> deleteUser(@Valid @RequestBody GwRequest<DeleteUserRequest> request) {
        log.info("UserController.deleteUser BEGIN ==> {}", JSONUtil.toJsonStr(request));

        boolean success = userService.deleteUser(request.getRequestData());
        GwResponse<String> response = null;
        if (success) {
            response = GwResponse.success("删除用户成功", null);
        } else {
            response = GwResponse.error("删除用户失败");
        }

        log.info("UserController.deleteUser END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

}
